package org.jeecgframework.web.controller;

import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.CbsAppMessageServiceI;
import org.jeecgframework.web.service.ZyCommonService;
import org.jeecgframework.web.system.pojo.base.*;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Scope("prototype")
@Controller
@RequestMapping("/zyCommonController")
public class ZyCommonController {

	@Autowired
	private SystemService systemService;
	@Autowired
	private ZyCommonService zyCommonService;
	@Autowired
	private CbsAppMessageServiceI cbsAppMessageService;
	
	
	/**
	 * 获取影院
	 * @param zyEquipmentSpare
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getCinemas")
	@ResponseBody
	public List<ZyCinemaEntity> getCinemas(ZyEquipmentSpareEntity zyEquipmentSpare, HttpServletRequest request) {
		List<ZyCinemaEntity> list = new ArrayList<ZyCinemaEntity>();
		try {
			list = zyCommonService.queryZyCinema();
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage());
		}
		return list;
	}
	
	/**
	 * @author xiul
	 * 2016-3-28 下午5:55:24
	 * Description 省市级联
	 * @param cityParentId 父级id
	 * @param level 级别 1 2 3
	 * @return json
	 */
	@RequestMapping(params = "queryTerritory")
	@ResponseBody
	public AjaxJson queryTerritory(
			@RequestParam(value="cityParentId",defaultValue="",required=false)String cityParentId,
			@RequestParam(value="level",defaultValue="")Short level){
		AjaxJson j = new AjaxJson();
		try {
			String sql = "SELECT * FROM t_s_territory WHERE territoryLevel = " + level;
			if(cityParentId.equals("")){
				sql += " AND ISNULL(territoryparentid) ";
			}else{
				sql += " AND territoryparentid = '" +cityParentId+ "'";
			}
			sql += "ORDER BY territorysort ";
			List<Map<String, Object>> rsMap = systemService.findForJdbc(sql);
//			System.err.println(sql);
			//List<TSTerritory> TSTerritorys = systemService.findListbySql(sql);
			j.setObj(rsMap);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			j.setMsg("获取省市信息失败！");
		}
		return j;
	}
	
	
	/**
	 * @author xiul
	 * 2016-4-18 上午9:51:16
	 * Description 根据组织机构id和联系人职位(岗位)查询对应人员
	 * @param request
	 * @param orgCode 组织机构code
	 * @param contactPosition 联系人职位
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params="queryTPServerBusinessr")
	public AjaxJson queryTPServerBusinessr(HttpServletRequest request,
			@RequestParam(value="orgCode")String orgCode,
			@RequestParam(value="contactPosition")String contactPosition){
		AjaxJson j = new AjaxJson();
		try {
			List<ZyContactsEntity> zyContacts = zyCommonService.queryUsersByConditions(orgCode, StaticValue.RULE_CODE_THIRDPARTY_HEADER, StaticValue.DIC_BUS_SCOPE_INSPECTION,  contactPosition);
			j.setObj(zyContacts);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-6-16 上午10:21:32
	 * Description 巡检计划流程记录
	 */
	@ResponseBody
	@RequestMapping(params="queryPlanRecord")
	public AjaxJson queryPlanRecord(@RequestParam(value="ipId")String ipId){
		AjaxJson j = new AjaxJson();
		List<Map<String,Object>> planRecordMapList = zyCommonService.queryPlanRecord(ipId);
		for (int i = 0; i < planRecordMapList.size(); i++){
			Map<String, Object> stringObjectMap = planRecordMapList.get(i);
			String fileName = "";
			String fileId = "";
			if(stringObjectMap.get("planState").equals(StaticValue.DIC_PLAN_STATE_THIRDPARTY_REFUSED)||
					stringObjectMap.get("planState").equals(StaticValue.DIC_PLAN_STATE_AREA_ENGINEER_REFUSED)||
							stringObjectMap.get("planState").equals(StaticValue.DIC_PLAN_STATE_ENGINEER_REFUSED)){
				Object object=stringObjectMap.get("id");
				if(null!=object&&!object.equals ( "" )){
					List<ZyFileLogEntity> zyFileLogEntity = systemService.findByProperty (ZyFileLogEntity.class,"businessKey",stringObjectMap.get("id").toString ());
					if (zyFileLogEntity.size ()>0) {
						stringObjectMap.put ( "fileName", zyFileLogEntity.get ( 0 ).getOldFileName());
						stringObjectMap.put ( "fileId", zyFileLogEntity.get ( 0 ).getId ());
					}else{
						stringObjectMap.put ( "fileName", "");
						stringObjectMap.put ( "fileId", "");
					}
				}else{
					stringObjectMap.put ( "fileName", "");
					stringObjectMap.put ( "fileId", "");
				}

			}else{
				stringObjectMap.put ( "fileName", "");
				stringObjectMap.put ( "fileId", "");
			}
		}
		j.setObj(planRecordMapList);
		return j;
	}

	/**
	 * @author xiul
	 * 2016-7-8 上午11:25:29
	 * Description 通用 搜索条件 选择BP页datagrid 跳转 
	 */
	@RequestMapping(params="goQueryBp")
	public ModelAndView goQueryBp(HttpServletRequest req){
		req.setAttribute("bpCodeId", oConvertUtils.getString(req.getParameter("bpCodeId")));
		return new ModelAndView("com/zybk/common/zyChooseBP");
	}
	
	/**
	 * @author xiul
	 * 2016-7-8 上午11:25:58
	 * Description 通用 搜索条件 选择BP页 datagird
	 */
	@RequestMapping(params="queryBpDatagrid")
	public void queryBpDatagrid(TSDepart tsDepart,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){
		CriteriaQuery cq = new CriteriaQuery(TSDepart.class, dataGrid);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tsDepart, request.getParameterMap());
		try{
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * @author xiul
	 * 2016-8-19 下午12:28:32
	 * Description 根据 tsTypeGroupCode 查询 对应的字典
	 * @param typeCode tsTypeGroupCode
	 */
	@ResponseBody
	@RequestMapping(params="queryTsTypesByTsTypegroupCode")
	public Object queryTsTypesByTsTypegroupCode(String typeCode){
		List<Map<String, Object>> mapList = new ArrayList<Map<String,Object>>();
		if(StringUtil.isNotEmpty(typeCode)){
			mapList = zyCommonService.queryTsTypesByTsTypegroupCode(typeCode);
		}
		return mapList;
	}

	/**
	 * @author xiul
	 * 2016-10-28 下午3:09:17
	 * Description 查询用户APP 未读的消息  
	 */
	@ResponseBody
	@RequestMapping(params="queryAppMessageUnreadCount")
	public AjaxJson queryAppMessageUnreadCount(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		Integer messageUnreadCount = cbsAppMessageService.queryAppMessageUnreadCount(req);
		j.setObj(messageUnreadCount);
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-8-24 上午11:14:18
	 * Description APP - 查询用户APP消息 
	 */
	@ResponseBody
	@RequestMapping(params="queryAppMessage")
	public AjaxJson queryAppMessage(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		List<Map<String, Object>> rsMap = cbsAppMessageService.queryAppMessage(req);
		j.setObj(rsMap);
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-10-26 下午8:07:03
	 * Description APP - 读消息 （修改消息状态，并返回消息）
	 */
	@ResponseBody
	@RequestMapping(params="appReadMessage")
	public AjaxJson appReadMessage(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		cbsAppMessageService.readAppMessage(j, req);
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-11-10 下午5:01:15
	 * Description APP 批量 把消息设为已读 
	 */
	@ResponseBody
	@RequestMapping(params="appBatchReadMesaage")
	public AjaxJson appBatchReadMessage(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		cbsAppMessageService.appBatchReadMessage(j,req);
		return j;
	}
	/**
	 * @author xiul
	 * 2016-11-1 下午5:51:26
	 * Description 查询当前登录用户组织机构下的工程师
	 */
	@ResponseBody
	@RequestMapping(params="queryCurrtDepartEngineer")
	public AjaxJson queryCurrtDepartEngineer(){
		AjaxJson j = new AjaxJson();
		try {
			zyCommonService.queryCurrtDepartEngineer(j);
		} catch (Exception e) {
			j.setSuccess(false);
			e.printStackTrace();
		}
		return j;
	}
}
